package com.ly.hotel.spainterface.jg.bean.gson.plugin;

import android.util.Log;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ThinkPad on 2018/2/3.
 */

public class XmlContentHandler extends DefaultHandler {

    private String nodeName;// 当前节点的名称；
    private StringBuilder version;
    private StringBuilder ip;
    private StringBuilder port;
    private StringBuilder showCategory;
    private StringBuilder limitedGoodsNum;
    private StringBuilder isShowSettingButton;
    private StringBuilder isOnlySee;
    private List<AppConfig> appConfigList;

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        version = new StringBuilder();
        ip = new StringBuilder();
        port = new StringBuilder();
        showCategory = new StringBuilder();
        limitedGoodsNum = new StringBuilder();
        isShowSettingButton = new StringBuilder();
        isOnlySee = new StringBuilder();
        appConfigList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        nodeName = localName;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String data = new String(ch,start,length).trim();
        if(nodeName.equals("version")){
            version.append(ch,start,length);
        }else if("ip".equals(nodeName)){
            ip.append(ch,start,length);
        }else if("port".equals(nodeName)){
            port.append(ch,start,length);
        }else if("showCategory".equals(nodeName)){
            showCategory.append(ch,start,length);
        }else if("limitedGoodsNum".equals(nodeName)){
            limitedGoodsNum.append(ch,start,length);
        }else if("isShowSettingButton".equals(nodeName)){
            isShowSettingButton.append(ch,start,length);
        }else if("isOnlySee".equals(nodeName)){
            isOnlySee.append(ch,start,length);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if("app".equals(localName)){
            Log.d("JgActivity","version=" + version.toString().trim());
            Log.d("JgActivity","ip=" + ip.toString().trim());
            Log.d("JgActivity","port=" + port.toString().trim());
            Log.d("JgActivity","showCategory=" + showCategory.toString().trim());
            Log.d("JgActivity","limitedGoodsNum=" + limitedGoodsNum.toString().trim());
            Log.d("JgActivity","isShowSettingButton=" + isShowSettingButton.toString().trim());
            Log.d("JgActivity","isOnlySee=" + isOnlySee.toString().trim());

            AppConfig appConfig = new AppConfig();
            appConfig.setVersion(version.toString().trim());
            appConfig.setIp(ip.toString().trim());
            appConfig.setPort(port.toString().trim());
            appConfig.setShowCategory(showCategory.toString().trim());
            appConfig.setLimitedGoodsNum(limitedGoodsNum.toString().trim());
            appConfig.setIsShowSettingButton(isShowSettingButton.toString().trim());
            appConfig.setIsOnlySee(isOnlySee.toString().trim());
            appConfigList.add(appConfig);

            version.setLength(0);
            ip.setLength(0);
            port.setLength(0);
            showCategory.setLength(0);
            limitedGoodsNum.setLength(0);
            isShowSettingButton.setLength(0);
            isOnlySee.setLength(0);
        }
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    public List<AppConfig> getAppConfigList() {
        return appConfigList;
    }
}
